import requests
from bs4 import BeautifulSoup


class baike:

    def __init__(self):
        self.url = "https://baike.baidu.com/item/%E5%91%A8%E6%9D%B0%E4%BC%A6/129156"
        self.headers = {'Accept': 'text/html, application/xhtml+xml, image/jxr, */*',
                        'Accept - Encoding': 'gzip, deflate',
                        'Accept-Language': 'zh,en-US;q=0.9,en;q=0.8',
                        'Connection': 'Keep-Alive',
                        'Host': 'baike.baidu.com',
                        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'}

    def start_down(self):
        print("正在下载....")
        html = requests.get(self.url, headers=self.headers, ).content
        return str(html, 'utf-8')

    def parse_html(self, html):
        print("下载完成.....")
        print("开始解析....")
        soup = BeautifulSoup(html, "html5lib")
        info_table = soup.select_one(".basicInfo-block")
        dls = info_table.find_all('dt')
        dds = info_table.find_all("dd")
        infos = []

        for i in range(0, len(dls)):
            dict = {}
            key = dls[i].get_text().replace('\xa0', "")
            value = dds[i].get_text().replace('\n', "")
            dict[key] = value
            infos.append(dict)
        return infos
